﻿using System;
using System.ServiceModel;
using System.ServiceModel.Activation;
using Itau.IM.Common.Utility;

namespace Itau.IM.Common.Service
{
    public class CustomHostFactory : ServiceHostFactory
    {
        protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {
            string sufixo = String.Empty;
            string ambiente = System.Environment.GetEnvironmentVariable("AMBIENTE_EXECUCAO");

            if (ambiente != null)
            {
                switch (ambiente)
                {
                    case "PRE":
                        sufixo = "Homologacao";
                        break;

                    case "PRO":
                        sufixo = "Producao";
                        break;

                    case "DES":
                        sufixo = "Desenvolvimento";
                        break;
                }

                Uri[] hosts = new Uri[1];

                foreach (Uri address in baseAddresses)
                {
                    hosts[0] = new Uri(String.Format("{0}://{1}:{2}{3}", address.Scheme, Configuration.GetSetting(String.Format("Service.BaseAddress.{0}", sufixo)), address.Port, address.LocalPath));

                    break;
                }

                return base.CreateServiceHost(serviceType, hosts);
            }
            else
            {
                return base.CreateServiceHost(serviceType, baseAddresses);
            }
        }
    }
}